<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="Includes/DBConn.jsp"%>

<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.Date"%>

<%@ page import="java.text.SimpleDateFormat"%>
<HTML>
<HEAD>
<TITLE>FusionCharts Free - Database + JavaScript Example</TITLE>
<%
	String jsVarString = "";

	//Database Objects
	Statement st1 = null, st2 = null;
	ResultSet rs1 = null, rs2 = null;

	String strQuery = "";

	int indexCount = -1;

	st1 = oConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
			ResultSet.CONCUR_READ_ONLY);

	//Create the query
	strQuery = "select * from Factory_Master";
	rs1 = st1.executeQuery(strQuery);

	String factoryId = null;
	String factoryName = null;

	String quantity = "";
	java.sql.Date date = null;
	java.util.Date uDate = null;
	String uDateStr = "";
	while (rs1.next()) {
		indexCount += 1;
		factoryId = rs1.getString("FactoryId");
		factoryName = rs1.getString("FactoryName");
		jsVarString += "\n\t\t\t\t\t" + "data[" + indexCount + "] = new Array();\n\r";

		strQuery = "select * from Factory_Output where FactoryId=" + factoryId + " order by DatePro Asc ";
		st2 = oConn.createStatement();
		rs2 = st2.executeQuery(strQuery);

		while (rs2.next()) {
			date = rs2.getDate("DatePro");
			quantity = rs2.getString("Quantity");
			if (date != null) {
				uDate = new java.util.Date(date.getTime());
				SimpleDateFormat sdf = new SimpleDateFormat("d/M");
				uDateStr = sdf.format(uDate);
			}
			jsVarString += "\t\t\t\t\t\t\t" + "data[" + indexCount + "].push(new Array('" + uDateStr + "'," + quantity + "));" + "\n\r";
		}
		try {
			if (null != rs2) {
				rs2.close();
				rs2 = null;
			}
		} catch (java.sql.SQLException e) {
			System.out.println("Could not close the resultset");
		}
		try {
			if (null != st2) {
				st2.close();
				st2 = null;
			}
		} catch (java.sql.SQLException e) {
			System.out.println("Could not close the statement");
		}
	}
%>
<SCRIPT LANGUAGE="Javascript" SRC="js/FusionCharts.js"></SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
var data = new Array();

<%/*Write the data as JavaScript variables here
The data is now present as arrays in JavaScript. Local JavaScript functions
can access it and make use of it. We'll see how to make use of it.
*/%>
<%=jsVarString%>
/** 
 * updateChart method is invoked when the user clicks on a pie slice.
 * In this method, we get the index of the factory, build the XML data
 * for that that factory, using data stored in data array, and finally
 * update the Column Chart.
 * @param	factoryIndex	Sequential Index of the factory.
 * @param	factoryName For display purpose
*/		
function updateChart(factoryIndex,factoryName){
	var FC_ColorCounter=0;
	var arr_FCColors= new Array("1941A5" , "AFD8F8", "F6BD0F", "8BBA00", "A66EDD", "F984A1", "CCCC00", "999999", "0099CC", "FF0000", "006F00", "0099FF", "FF66CC", "669966", "7C7CB4", "FF9933", "9900FF", "99FFCC", "CCCCFF", "669900");
	var strXML = "<graph caption='" + factoryName  + " Output ' subcaption='(In Units)' xAxisName='Date' decimalPrecision='0'>";
	var i=0;
	for (i=0; i<data[factoryIndex].length; i++){
		strXML = strXML + "<set name='" + data[factoryIndex][i][0] + "' value='" + data[factoryIndex][i][1] + "' color='"+ arr_FCColors[++FC_ColorCounter % arr_FCColors.length] +"' />";
	}
	strXML = strXML + "</graph>";
	updateChartXML("FactoryDetailed",strXML);
}
</SCRIPT>
<style type="text/css">
<!--
body {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}

.text {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>
</HEAD>
<BODY>
	<CENTER>
		<%
			String strXML = "";
			String totalOutput = "";
			indexCount = -1;
			strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1' numberSuffix=' Units' pieSliceDepth='20' formatNumberScale='0'>";
			rs1.beforeFirst();
			while (rs1.next()) {
				indexCount = indexCount + 1;

				factoryId = rs1.getString("FactoryId");
				factoryName = rs1.getString("FactoryName");

				strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId="
						+ factoryId;
				st2 = oConn.createStatement();
				rs2 = st2.executeQuery(strQuery);
				if (rs2.next()) {
					totalOutput = rs2.getString("TotOutput");
					
					strXML += "<set name='" + factoryName + "' value='"
							+ totalOutput + "' link='javaScript:updateChart("
							+ indexCount + ",&quot;" + factoryName + "&quot;)'/>";
					try {
						if (null != rs2) {
							rs2.close();
							rs2 = null;
						}
					} catch (java.sql.SQLException e) {
						System.out.println("Could not close the resultset");
					}
					try {
						if (null != st2) {
							st2.close();
							st2 = null;
						}
					} catch (java.sql.SQLException e) {
						System.out.println("Could not close the statement");
					}
				}
			}
			strXML += "</graph>";
			
			try {
				if (null != rs1) {
					rs1.close();
					rs1 = null;
				}
			} catch (java.sql.SQLException e) {
				System.out.println("Could not close the resultset");
			}
			try {
				if (null != st1) {
					st1.close();
					st1 = null;
				}
			} catch (java.sql.SQLException e) {
				System.out.println("Could not close the statement");
			}
			try {
				if (null != oConn) {
					oConn.close();
					oConn = null;
				}
			} catch (java.sql.SQLException e) {
				System.out.println("Could not close the connection");
			}
		%>
		<jsp:include page="Includes/FusionChartsRenderer.jsp" flush="true">
			<jsp:param name="chartSWF" value="FusionCharts/FCF_Pie3D.swf" />
			<jsp:param name="strURL" value="" />
			<jsp:param name="strXML" value="<%=strXML %>" />
			<jsp:param name="chartId" value="FactorySum" />
			<jsp:param name="chartWidth" value="650" />
			<jsp:param name="chartHeight" value="300" />
			<jsp:param name="debugMode" value="false" />
			<jsp:param name="registerWithJS" value="false" />
		</jsp:include>
		<BR>
		<jsp:include page="Includes/FusionChartsRenderer.jsp" flush="true">
			<jsp:param name="chartSWF"
				value="FusionCharts/FCF_Column2D.swf?ChartNoDataText=Please select a factory from pie chart above to view detailed data." />
			<jsp:param name="strURL" value="" />
			<jsp:param name="strXML" value="<graph></graph>" />
			<jsp:param name="chartId" value="FactoryDetailed" />
			<jsp:param name="chartWidth" value="600" />
			<jsp:param name="chartHeight" value="300" />
			<jsp:param name="debugMode" value="false" />
			<jsp:param name="registerWithJS" value="false" />
		</jsp:include>
	</CENTER>
</BODY>
</HTML>
